import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.util.Collections;

/**
 * @author: mornd
 * @dateTime: 2023/7/22 - 13:59
 * mp 代码生成
 */
public class Main {

    @Test
    public void test() {
        String arr = "oms_order,oms_order_item,oms_order_operate_history" +
                ",oms_order_return_apply,oms_order_return_reason,oms_order_setting" +
                ",oms_payment_info,oms_refund_info";

        String moduleName = "order";
        String tablePrefix = "oms_";

        generate(arr.split(","), moduleName, tablePrefix);
    }

    public void generate(String[] tableName, String moduleName, String tablePrefix) {
        String url = "jdbc:mysql://localhost:3306/gulimall_oms";
//        String dir = "D://gulimall_generator";
        String dir = "E:\\idea_work_space2023\\gulimall-all\\gulimall-parent\\gulimall-order\\src\\main\\java";
        String mapperDir = "E:\\idea_work_space2023\\gulimall-all\\gulimall-parent\\gulimall-order\\src\\main\\resources\\mapper";

        FastAutoGenerator.create(url, "root", "root115")
                .globalConfig(builder -> {
                    builder.author("mornd") // 设置作者
                            .fileOverride() // // 覆盖已生成文件
                            .outputDir(dir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.mornd.gulimall") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperDir)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok();
                    builder.controllerBuilder().enableRestStyle();

                    builder.mapperBuilder()
                            .enableMapperAnnotation()
                            .enableBaseResultMap();
                            //.enableBaseColumnList(); // base columns
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix(tablePrefix); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
